<template>
    <div>
        <navtitle title="注册豆瓣" />
        <van-form @submit="onSubmit">
            <van-cell-group inset>
                <van-field v-model="username" name="username" label="手机号" placeholder="手机号"
                    :rules="[{ pattern, message: '请输入正确内容' }]" />
                <van-field v-model="password" type="password" name="password" label="密码" placeholder="密码"
                    :rules="[{ required: true, message: '请填写密码' }]" />

            </van-cell-group>
            <div style="margin: 16px;">
                <van-button round block type="primary" native-type="submit">
                    登录
                </van-button>
                <van-button round block native-type="submit" @click="onpass">
                    注册
                </van-button>
            </div>
        </van-form>
        <div>
            <span>账号密码登录</span><b>验证码收不到?</b>
        </div>
    </div>
</template>

<script setup>
import navtitle from '@/components/navtitle.vue';
import { ref } from 'vue'
const username = ref('');
const password = ref('');
import { useRoute, useRouter } from 'vue-router'

const router = useRouter()
const pattern = /^1[3-9]\d{9}$/
import { loginApi } from '@/api/api';
import { showSuccessToast } from 'vant';
const onSubmit = () => {
    loginApi(username.value, password.value).then((res) => {
        console.log('登录数据', res);
        if (res.data.code == 0) {
            showSuccessToast('登录成功')
            router.push('/home')
            localStorage.setItem('token', res.data.data.token)
        }
    })
}
const onpass = () => {
    router.push('/create')
}
</script>

<style lang="scss" scoped>
div {
    .van-button {
        margin-top: 10px;
    }
}
div {
    b{
        float: right;
    }
    span{
        color: rgb(28, 224, 28);
    }
}
</style>